package com.example.chasshelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chasshelper.adapter.MessageAdapter;
import com.example.chasshelper.adapter.TestAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Message;
import com.example.chasshelper.model.Registration;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.DaoUtils;

import java.util.List;

public class TestStuActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = TestStuActivity.class.getSimpleName();
    private final Activity mContext = TestStuActivity.this;
    //控件
    private RecyclerView rv_testList;
    private Button btn_back;
    private Button btn_submit;
    private Button btn_save;
    //adapter
    private TestAdapter testAdapter;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private Student student;
    private List<Test> testList;
    private Classroom classroom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_stu);
        initUI();
        initData();
    }
    /*
     * 界面初始化
     * */
    private void initUI() {
        //绑定控件
        rv_testList = findViewById(R.id.rv_testList);
        btn_back = findViewById(R.id.btn_back);
        btn_submit = findViewById(R.id.btn_submit);
        btn_save = findViewById(R.id.btn_save);
        //点击事件
        btn_back.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    /*
     * 数据初始化
     * */
    private void initData() {
        //接收数据
        student = (Student) getIntent().getSerializableExtra("student");
        classroom = (Classroom) getIntent().getSerializableExtra("classroom");
        testList = classroomDao.findTest(classroom);
        //回显
        //adapter初始化，并向adapter传递数据
        testAdapter = new TestAdapter(mContext,testList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rv_testList.setLayoutManager(linearLayoutManager);
        rv_testList.setAdapter(testAdapter);

    }

    //点击事件
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_save://暂存
                for (int position = 0; position < rv_testList.getLayoutManager().getItemCount(); position++) {
                    System.out.println(getONeAnswer(position));
                    solutionDao.submit(classroom,student,testList.get(position),getONeAnswer(position),"暂存");
                }
                break;
            case R.id.btn_submit://提交
                for (int position = 0; position < rv_testList.getLayoutManager().getItemCount(); position++) {
                    System.out.println(getONeAnswer(position));
                    solutionDao.submit(classroom,student,testList.get(position),getONeAnswer(position),"提交");
                }
                finish();
                break;
        }
    }

    public String getONeAnswer(int position) {
        //获取题目类型
        View view =  rv_testList.getChildAt(position);
        TextView textView = (TextView) view.findViewById(R.id.tv_type);
        String type = textView.getText().toString();
        String answer="";
        if(type.equals("判断")) {
            RadioButton rb_option1 = view.findViewById(R.id.rb_option1);
            RadioButton rb_option2 = view.findViewById(R.id.rb_option2);
            if (rb_option1.isChecked()) {
                answer= "A";
            } else if((rb_option2.isChecked())) {
                answer= "B";
            }
        }
        if (type.equals("单选")) {
            RadioButton rb_option1 = view.findViewById(R.id.rb_option1);
            RadioButton rb_option2 = view.findViewById(R.id.rb_option2);
            RadioButton rb_option3 = view.findViewById(R.id.rb_option3);
            RadioButton rb_option4 = view.findViewById(R.id.rb_option4);
            RadioButton rb_option5 = view.findViewById(R.id.rb_option5);
            if (rb_option1.isChecked()) {
                answer= "A";
            } else if((rb_option2.isChecked())) {
                answer= "B";
            }else if((rb_option3.isChecked())) {
                answer= "C";
            }else if((rb_option4.isChecked())) {
                answer= "D";
            }else if((rb_option5.isChecked())) {
                answer= "E";
            }
        }
        if (type.equals("多选")) {
            CheckBox cb_option1 = view.findViewById(R.id.cb_option1);
            CheckBox cb_option2 = view.findViewById(R.id.cb_option2);
            CheckBox cb_option3 = view.findViewById(R.id.cb_option3);
            CheckBox cb_option4 = view.findViewById(R.id.cb_option4);
            CheckBox cb_option5 = view.findViewById(R.id.cb_option5);
            if (cb_option1.isChecked()) {
                answer+= "A";
            }
            if((cb_option2.isChecked())) {
                answer+= "B";
            }
            if((cb_option3.isChecked())) {
                answer+= "C";
            }
            if((cb_option4.isChecked())) {
                answer+= "D";
            }
            if((cb_option5.isChecked())) {
                answer+= "E";
            }
        }
        return answer;
    }
}